// pages/dish/togetherIndex/togetherIndex.js
const app = getApp()
var server = require('../../../utils/server');
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tu: app.globalData.tupian,
    dj: 1,
    list: [],
    PageIndex: 1,
    count: 0,
    hasNext: false,
    search: '^',
    inp: '',
    shouquan: 0,


    mellData: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.isAuth();
  },
  onShow: function(e) {
    // that.setData({
    //   list:[],
    //   PageIndex:1
    // })
    // that.guanzu(1)
  },
  guanzu: function(PageIndex, search) {
    var list = that.data.list
    server.requestForm(false, 'get', 'user', {
        filter: {
          _id: app.globalData.userID
        },
        sort: {
          createTime: -1
        }
      },
      function(e) {
        console.log(e)
        if (!e.data[0].careID) {
          console.log('jinlai')
          var careID = []
        } else {
          var careID = e.data[0].careID
        }
        server.requestForm(false, 'post', 'card/getCare', {
            page: {
              size: 10,
              index: PageIndex
            },
            filter: {
              careID: careID,
              search: search
            }
          },
          function(e) {
            console.log(e)
            if (e.code == 0) {
              for (var i in e.data.items) {
                list.push(e.data.items[i])
              }
              that.setData({
                list: list,
                hasNext: e.data.hasNext,
                count: 0
              })
              if (PageIndex == 1 && e.data.count == 0) {
                that.setData({
                  count: 1
                })
              }
            } else {
              wx.showToast({
                title: e.message,
                icon: 'none',
                duration: 1000,
              });
            }

          })
      })
  },

  fujin: function(PageIndex, search) {
    var list = that.data.list
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        console.log(res)
        server.requestForm(false, 'get', 'card/nearby', {
            page: {
              size: 5,
              index: PageIndex
            },
            // sort: {
            //   createTime: -1
            // },
            filter: {
              latitude: res.latitude,
              longitude: res.longitude,
              search: search
            }
          },
          function(e) {
            console.log(e)
            if (e.code == 0) {
              for (let i = 0; i < e.data.items.length; i++) {
                list.push(e.data.items[i])
                e.data.items[i].distance = Math.round(e.data.items[i].distance * 100 * (6371 * 3.14159265359 / 180));
              }
              that.setData({
                list: list,
                hasNext: e.data.hasNext
              })
            } else {
              wx.showToast({
                title: e.message,
                icon: 'none',
                duration: 1000,
              });
            }

          })
      },
      fail(res) {
        console.log('shibai', res)
        if (res.errMsg == "getLocation:fail authorize no response") {
          wx.showToast({
            title: '请在设置中打开位置授权',
            icon: 'none',
            duration: 1000,
            mask: true
          });
        }
      }
    })
  },
  dj: function(e) {
    var dj = e.currentTarget.dataset.dd
    console.log(dj)
    that.setData({
      dj: dj,
      count: 0,
      PageIndex: 1,
      list: [],
      search: '^',
      inp: ''
    })
    if (dj == 2) {
      if (app.globalData.userID == '') {
        that.setData({
          shouquan: 1
        })
      } else {
        that.fujin(1, '^')
      }

    } else {
      if (app.globalData.userID == '') {
        that.setData({
          shouquan: 1
        })
      } else {
        that.guanzu(1, '^')
      }

    }
  },
  //搜索
  ss: function(e) {
    console.log(e)
    var search = e.detail.value
    if (search == '') {
      that.setData({
        search: '^'
      })
    } else {
      that.setData({
        search: search
      })
    }
  },
  sous: function(e) {
    if (app.globalData.userID == '') {
      wx.showToast({
        title: '请先授权登录',
        icon: 'none',
        duration: 1000,
      });
    } else {
      that.setData({
        list: [],
      })
      if (that.data.dj == 1) {
        that.guanzu(that.data.PageIndex, that.data.search)
      } else {
        that.fujin(that.data.PageIndex, that.data.search)
      }
    }

  },
  fans: function(e) {
    wx.navigateTo({
      url: '../fans/fans',
    })
  },
  carddetails: function(e) {
    console.log(e)
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../carddetails/carddetails?id=' + id,
    })
  },
  sy: function(e) {
    wx.reLaunch({
      url: '../index/index',
    })
  },
  sys: function(e) {
    wx.reLaunch({
      url: '../laboratory/laboratory',
    })
  },
  xx: function(e) {
    wx.reLaunch({
      url: '../news/news',
    })
  },
  my: function(e) {
    wx.reLaunch({
      url: '../mine/my/my',
    })
  },
  onReachBottom: function(e) {
    if (that.data.hasNext) {
      that.setData({
        PageIndex: that.data.PageIndex + 1
      })
      console.log(that.data)
      if (that.data.dj == 1) {
        that.guanzu(that.data.PageIndex, that.data.search)
      } else {
        that.fujin(that.data.PageIndex, that.data.search)
        // that.sc(that.data.PageIndex, that.data.id)
      }
    }
  },
  isAuth(){
    let that=this;
    wx.getSetting({
      withSubscriptions: true,
      success(res){
        if(!res.authSetting['scope.userLocation']){
          wx.showModal({
            title:'提示',
            content:'请求获取位置权限',
            success(res){
              if(res.confirm==false){
                return false
              }else{
                that.yesAuth();
              }
            }
          })
        }else{
          that.yesAuth();
        }
      }
    })
  },

  yesAuth: function() {
    let that = this;
    wx.getLocation({
      type: 'gcj02',
      isHighAccuracy: true,
      success: function(res) {
        console.log(res) // 获取到经纬度信息
        let location = {
          latitude: res.latitude,
          longitude: res.longitude
        }
        that.getStoreInfo(location)
        wx.setStorageSync('location', location)
      },
      fail: function(err) {
        console.log(err)
      }
    })
  },

  getStoreInfo(location){
    server.requestForm(false,'get','location',location,(res) => {
      this.setData({
        mellData: res.data[0]
      })
    })

    // server.requestForm(false, 'get', 'mall', {}, function(res) {
    //   console.log("1231231231231231223", res.data[0]);
    // });
  },
})